package jmu.edu.yjc.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jmu.edu.yjc.Utils.BeanCopyUtil;
import jmu.edu.yjc.constants.SystemConstants;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.Healthy;
import jmu.edu.yjc.domain.vo.PageVo;
import jmu.edu.yjc.domain.vo.elderly.ElderlyVo;
import jmu.edu.yjc.domain.vo.elderly.ExcelElderlyVo;
import jmu.edu.yjc.domain.vo.healthy.ExcelHealthyUploadVo;
import jmu.edu.yjc.domain.vo.healthy.ExcelHealthyVo;
import jmu.edu.yjc.domain.vo.healthy.HealthyMessageVo;
import jmu.edu.yjc.domain.vo.healthy.HealthyVo;
import jmu.edu.yjc.interfaces.SaveInterface;
import jmu.edu.yjc.listener.MyExcelListener;
import jmu.edu.yjc.mapper.HealthyMapper;
import jmu.edu.yjc.service.ElderlyService;
import jmu.edu.yjc.service.HealthyService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 健康表(Healthy)表服务实现类
 *
 * @author makejava
 * @since 2023-04-05 21:50:01
 */
@Service("healthyService")
public class HealthyServiceImpl extends ServiceImpl<HealthyMapper, Healthy> implements HealthyService, SaveInterface<ExcelHealthyUploadVo> {

    @Resource
    private ExportService exportService;

    @Resource
    private ElderlyService elderlyService;
    @Override
    public ResponseResult listHealthy(Integer pageSize, Integer pageNum, String name, String startTime, String endTime) {
        LambdaQueryWrapper<Healthy> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name),Healthy::getName,name)
                .or()
                .between(StringUtils.hasText(startTime)&&StringUtils.hasText(endTime),Healthy::getMeasurementTime,startTime,endTime);
        wrapper.orderByDesc(Healthy::getMeasurementTime);
        Page<Healthy> page = new Page<>(pageNum,pageSize);
        page(page,wrapper);
        return ResponseResult.okResult(new PageVo(page.getRecords(),page.getTotal()));
    }

    @Override
    public void export(HttpServletResponse response) {
        List<ExcelHealthyVo> healthyList = baseMapper.listCurrentDayData();
        exportService.export(response,"当天健康数据.xlsx",healthyList, ExcelHealthyVo.class);
    }

    @Override
    public ResponseResult uploadExcel(MultipartFile file) {
        try {
            //headRowNumber 2 代表从第3行开始导入 第一行为大标题 第二行为表头
            EasyExcel.read(file.getInputStream(), ExcelHealthyUploadVo.class,
                    new MyExcelListener(this)).headRowNumber(1).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(-1,"上传失败" + e.getMessage());
        }
        return ResponseResult.okResult();
    }

    @Override
    public List<HealthyMessageVo> queryByAnPushStatus() {
        LambdaQueryWrapper<Healthy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Healthy::getPushStatus,SystemConstants.PUSH_STATUS_UNPUSH);
        List<HealthyMessageVo> healthyMessageVos = BeanCopyUtil.copyBeanList(list(wrapper), HealthyMessageVo.class);
        for (HealthyMessageVo messageVo : healthyMessageVos) {
            ElderlyVo elderlyVo = elderlyService.queryByIdentityId(messageVo.getIdentityId());
            messageVo.setElderly(elderlyVo);
        }
        return healthyMessageVos;
    }

    @Override
    public ResponseResult getDangerElderly(Integer pageNum, Integer pageSize) {
        Integer res = (pageNum-1)*pageSize;
        Integer tar = (pageNum)*pageSize;
        List<Healthy> healthyList = baseMapper.getDangerElderly(res,tar);
        return ResponseResult.okResult(healthyList);
    }

    @Override
    public void save(List<ExcelHealthyUploadVo> list, Map<String, Object> param) {
        List<ExcelHealthyVo> excelHealthyVos = baseMapper.listCurrentDayData();
        List<Healthy> saveEntity = new ArrayList<>();
        for (ExcelHealthyUploadVo vo : list) {
            boolean is_exist = false;
            Healthy healthy = BeanCopyUtil.copyBeans(vo, Healthy.class);
            for (ExcelHealthyVo healthyVo : excelHealthyVos) {
                //今天已经上传过
                if(healthyVo.getName().equals(vo.getName())&&healthyVo.getIdentityId().equals(vo.getIdentityId())) {
                    is_exist = true;
                    break;
                }
            }
            if(!is_exist) {
                healthy.setMeasurementTime(new Date());
                saveEntity.add(healthy);
            }
        }
        saveBatch(saveEntity);
    }
}
